import type {App} from 'vue'
import {createRouter, createWebHashHistory} from 'vue-router'
import createRouters from './routePath'
// import store from "@/store";

const router = createRouter({
    history: createWebHashHistory(),
    routes: createRouters
})

const whiteList = ['/index']

// router.beforeEach((to: any, from: any, next: any) => {
//     ElMessage.closeAll();
//     const hasToken = !!store.state
//     const hasSiteId = !!store.state
//     if (hasToken || hasSiteId) {
//         // 有token
//         next()
//     } else {
//         // 没有token
//         if (whiteList.indexOf(to.path) !== -1) {
//             // 在免登录白名单，直接进入
//             next()
//         } else {
//             // next(`/index?redirect=${to.fullPath}`) // 否则全部重定向到登录页
//             let url = `/views/globalCfgs/site`
//             if (!hasToken) url = `/login`
//             next(url)
//         }
//     }
// })

router.afterEach(() => {
})

// 删除/重置路由
export function resetgudingRoute(): void {
    router.getRoutes().forEach((route) => {
        const {name}: any = route
        let flag = false
        createRouters.forEach((route) => {
            if (route.name == name) {
                flag = true
            }
        })
        if (!flag) {
            router.hasRoute(name) && router.removeRoute(name)
        } else {
            router.addRoute(route)
        }
    })
}

export function setupRouter(app: App<Element>): void {
    app.use(router)
}

export default router
